<?php
require_once 'db.php';

// 获取用户IP
function get_user_ip() {
    if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
        return $_SERVER['HTTP_CLIENT_IP'];
    } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        return $_SERVER['HTTP_X_FORWARDED_FOR'];
    } else {
        return $_SERVER['REMOTE_ADDR'];
    }
}

// 检查IP是否被封禁
function is_ip_banned($ip) {
    global $pdo;
    $stmt = $pdo->prepare("SELECT id FROM banned_ips WHERE ip_address = ?");
    $stmt->execute([$ip]);
    return $stmt->rowCount() > 0;
}

// 检查内容是否包含违禁词
function has_prohibited_words($content) {
    global $pdo;
    $stmt = $pdo->query("SELECT word FROM prohibited_words");
    $words = $stmt->fetchAll(PDO::FETCH_COLUMN);
    
    foreach ($words as $word) {
        if (stripos($content, $word) !== false) {
            return true;
        }
    }
    return false;
}

// 获取违禁词列表
function get_prohibited_words() {
    global $pdo;
    $stmt = $pdo->query("SELECT word FROM prohibited_words");
    return $stmt->fetchAll(PDO::FETCH_COLUMN);
}

// 获取系统设置
function get_setting($name, $default = '') {
    global $pdo;
    $stmt = $pdo->prepare("SELECT value FROM settings WHERE name = ?");
    $stmt->execute([$name]);
    $result = $stmt->fetch();
    return $result ? $result['value'] : $default;
}

// 更新系统设置
function update_setting($name, $value) {
    global $pdo;
    $stmt = $pdo->prepare("INSERT INTO settings (name, value) VALUES (?, ?) ON DUPLICATE KEY UPDATE value = ?");
    return $stmt->execute([$name, $value, $value]);
}

// 检查是否已点赞
function has_liked($type, $target_id, $ip) {
    global $pdo;
    $stmt = $pdo->prepare("SELECT id FROM likes WHERE type = ? AND target_id = ? AND ip_address = ?");
    $stmt->execute([$type, $target_id, $ip]);
    return $stmt->rowCount() > 0;
}

// 获取点赞数
function get_like_count($type, $target_id) {
    global $pdo;
    $stmt = $pdo->prepare("SELECT COUNT(*) as count FROM likes WHERE type = ? AND target_id = ?");
    $stmt->execute([$type, $target_id]);
    $result = $stmt->fetch();
    return $result['count'];
}

// 获取评论数
function get_comment_count($type, $target_id) {
    global $pdo;
    $stmt = $pdo->prepare("SELECT COUNT(*) as count FROM comments WHERE type = ? AND target_id = ?");
    $stmt->execute([$type, $target_id]);
    $result = $stmt->fetch();
    return $result['count'];
}

// 验证图片上传
function validate_image_upload($file) {
    if ($file['error'] != UPLOAD_ERR_OK) {
        return "图片上传失败，请重试";
    }
    
    if (!in_array($file['type'], ALLOWED_IMAGE_TYPES)) {
        return "不支持的图片格式，仅支持JPG、PNG、GIF";
    }
    
    if ($file['size'] > MAX_IMAGE_SIZE) {
        return "图片大小不能超过5MB";
    }
    
    return true;
}

// 上传图片
function upload_image($file,$t=1) {
    $validation = validate_image_upload($file);
    if ($validation !== true) {
        return ['success' => false, 'message' => $validation];
    }
    
    $filename = uniqid() . '.' . pathinfo($file['name'], PATHINFO_EXTENSION);
    if($t == 2){
        $target_path = '../'.UPLOAD_DIR . $filename;
    }else{
        $target_path = UPLOAD_DIR . $filename;
    }
    
    if (move_uploaded_file($file['tmp_name'], $target_path)) {
        return ['success' => true, 'filename' => $filename];
    } else {
        return ['success' => false, 'message' => '图片保存失败，请重试'];
    }
}

// 管理员登录验证
function admin_login($username, $password) {
    global $pdo;
    $stmt = $pdo->prepare("SELECT * FROM admins WHERE username = ?");
    $stmt->execute([$username]);
    $admin = $stmt->fetch();
    
    if ($admin && password_verify($password, $admin['password'])) {
        $_SESSION['admin_logged_in'] = true;
        $_SESSION['admin_id'] = $admin['id'];
        return true;
    }
    return false;
}

// 检查管理员是否已登录
function is_admin_logged_in() {
    return isset($_SESSION['admin_logged_in']) && $_SESSION['admin_logged_in'] === true;
}

// 管理员登出
function admin_logout() {
    unset($_SESSION['admin_logged_in']);
    unset($_SESSION['admin_id']);
    session_destroy();
}
?>
